//
//  HWXigaiMiMaPasswordViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/11/26.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWXigaiMiMaPasswordViewController.h"
#import "HWGuanlianPhoneTableViewCell.h"
#import "Countdown.h"

@interface HWXigaiMiMaPasswordViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) UIView *lowerPartView;
@property(strong,nonatomic) UITextField *oneTextField;  // 第一个
@property(strong,nonatomic) UITextField *twoTextfeild;  //第二个
@property(strong,nonatomic) UITextField *threderTextFeild; // 第三个
@property(strong,nonatomic) NSString *dataStr; // 验证码返回的参数
@end

@implementation HWXigaiMiMaPasswordViewController

-(UITableView *)mainaTableVie {

    if  (_mainaTableVie == nil) {

        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, CF_NavHeight+44*4) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
       // _mainaTableVie.backgroundColor = RGB(241, 242, 244);
        
        
        
    }
    return   _mainaTableVie ;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
    
}


//设置UI
-(void)setupUI {
    self.navigationItem.title = @"忘记密码";
    self.navigationItem.title = [HWjianzhiduiObject keylocalization: @"忘记密码"];
    
    
    self.dataStr = @"";
    self.view.backgroundColor = [UIColor whiteColor];
   // self.view.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.mainaTableVie];
    [self addLowerPartView];
    
  
    

}


// 发送邮箱验证吗 或 手机号验证码

- (void)guanlianyouxiang {
    
    
    if ([self.panduanStr isEqualToString:@"1"]) {
        [self shoujiHaoYanzhengma];
        
    } else {
        
        
        [self youxiangyanzhengMa];
        
        
        
    }


//     NSString *userID = [HWLocallyStoredData getLoginUserID];
//
//    int t =  [WMUtil getRandomNumber:101 to:998];
//     NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
//     NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
//     NSString *signMD5 = [WMUtil md5HexDigest:sign];
//     NSDictionary *parametes = @{
//                                @"ct" :  @"app",
//                                @"ac" : @"mobile_code",
//                                @"ios" : @"1",
//                                @"t" :tStr,
//                                @"sign": signMD5,
//                                @"phone": self.oneTextField.text
//                                };
//    [MBProgressHUD show:@"正在发送验证码"];
//    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
//
//
//        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
//     //   [MBProgressHUD hideHUD];
//
//
//        if ([codeStr intValue] == 1) {
//
//            //  response[@"data"];
//
//          //  [MBProgressHUD showSuccess:@"验证码发送成功"];
//
//            [MBProgressHUD  showSuccess:response[@"message"]];
//            self.dataStr =  [NSString stringWithFormat:@"%@",response[@"data"]];
//
//
//
//
//        } else {
//            //  self.dataStr= @"";
//             [MBProgressHUD  showError:response[@"message"]];
//
//        }
//        NSLog(@"%@",response);
//
//
//    } failure:^(NSString *error) {
//
//        [MBProgressHUD  showError:@"验证码发送失败"];
//       // self.dataStr= @"";
//
//    }];

    
    
}



// 手机号发送验证码
- (void)shoujiHaoYanzhengma {
    
     NSString *userID = [HWLocallyStoredData getLoginUserID];
      
     int t =  [WMUtil getRandomNumber:101 to:998];
      NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
//      NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
//      NSString *signMD5 = [WMUtil md5HexDigest:sign];
      NSDictionary *parametes = @{
                                 @"ct" :  @"app",
                                 @"ac" : @"mobile_code",
                                 @"ios" : @"1",
                                 @"type" : @"1",
                                 @"phone": self.oneTextField.text
                                 };
     [MBProgressHUD show:@"正在发送验证码"];
     [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {


         NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
      //   [MBProgressHUD hideHUD];


         if ([codeStr intValue] == 1) {

             //  response[@"data"];

           //  [MBProgressHUD showSuccess:@"验证码发送成功"];

             [MBProgressHUD  showSuccess:response[@"message"]];
             self.dataStr =  [NSString stringWithFormat:@"%@",response[@"data"]];




         } else {
             //  self.dataStr= @"";
              [MBProgressHUD  showError:response[@"message"]];

         }
         NSLog(@"%@",response);


     } failure:^(NSString *error) {

         [MBProgressHUD  showError:@"验证码发送失败"];
        // self.dataStr= @"";

     }];

    
}



// 邮箱验证码
- (void)youxiangyanzhengMa {
    
    
    if (![WMUtil validateEmail:self.oneTextField.text]) {
        
        [MBProgressHUD showError:@"请输入正确的邮箱"];
        
        return;
        
    }
    

        NSDictionary *parametes = @{
                                    @"ct" :  @"app",
                                    @"ac" : @"email_code",
                                    @"ios" : @"1",
                                    @"type" : @"1",
                                    @"email": self.oneTextField.text
                                    };
    
    
    [MBProgressHUD show:@"正在发送验证码"];
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
    
    
            NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
         //   [MBProgressHUD hideHUD];
    
    
            if ([codeStr intValue] == 1) {
    
                //  response[@"data"];
    
              //  [MBProgressHUD showSuccess:@"验证码发送成功"];
    
                [MBProgressHUD  showSuccess:response[@"message"]];
                self.dataStr =  [NSString stringWithFormat:@"%@",response[@"data"]];
    
    
    
    
            } else {
                //  self.dataStr= @"";
                 [MBProgressHUD  showError:response[@"message"]];
    
            }
            NSLog(@"%@",response);
    
    
        } failure:^(NSString *error) {
    
            [MBProgressHUD  showError:@"验证码发送失败"];
           // self.dataStr= @"";
    
        }];

    
    
    
    
}














// 添加低部UIview
- (void)addLowerPartView {
    
    self.lowerPartView = [[UIView alloc]initWithFrame:CGRectMake(0, CF_NavHeight+44*3+30, kScreenW, 44)];
    // self.lowerPartView.backgroundColor = [UIColor yellowColor];
    self.lowerPartView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.lowerPartView];
    
    NSString *queding = [HWjianzhiduiObject keylocalization:@"确定"];
    
    UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(15, 0, kScreenW-15*2, 36) Title:queding Image:nil Font:13 Color:RGB(255, 255, 155) Target:self Selector:@selector(buttonTap)];
    butt.backgroundColor = blueBackgroundBT;
    butt.layer.cornerRadius = 18;
    butt.layer.masksToBounds = YES;
    [self.lowerPartView addSubview:butt];
    
    
    
}

- (void)buttonTap {
    
    NSLog(@"保存");

      
    if ([self.panduanStr isEqualToString:@"1"]) {
        [self shoujigaimiama];
        
    } else  {
        
        [self youxianggaimian];
        
        
        
    }
       
    
    
    
}
// 手机改密码
- (void)shoujigaimiama {
    
    
    if (!(self.oneTextField.text.length > 0)) {
          
          
             [MBProgressHUD showError:@"手机不能为空"];
             return;
    }else if (!(self.twoTextfeild.text.length > 0)) {
          
          
        [MBProgressHUD showError:@"验证码不能为空"];
        return;
          
          
    } else if (!(self.threderTextFeild.text > 0)){
        
         [MBProgressHUD showError:@"密码不能为空"];
               return;
        
    }
      
      
      NSString *userID = [HWLocallyStoredData getLoginUserID];
      NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
      NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
      NSString *signMD5 = [WMUtil md5HexDigest:sign];
    
      NSDictionary *parametes = @{
                                   @"ct" :  @"app",
                                   @"ac" : @"re_passwd",
                                   @"ios" : @"1",
                                   @"phone": self.oneTextField.text,
                                   @"code" : self.twoTextfeild.text,
                                   @"sessionid" : self.dataStr,
                                   @"passwd": self.threderTextFeild.text
                                   };
    

       [MBProgressHUD show:@"正在修改密码"];
       
       [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
           
         
           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        //   [MBProgressHUD hideHUD];
           
           
           if ([codeStr intValue] == 1) {
             
         
              [MBProgressHUD  showSuccess:response[@"message"]];
            
             //
                 NSArray *Controller =   self.navigationController.childViewControllers;
                 [self.navigationController popToViewController:Controller[Controller.count-3] animated:YES];
               
             //   [self.navigationController popViewControllerAnimated:YES];
               
               

           } else {
            
                [MBProgressHUD  showSuccess:response[@"message"]];
               
           }
           NSLog(@"%@",response);
           
           
       } failure:^(NSString *error) {

           [MBProgressHUD  showError:@"修改成功"];

       }];
    
    
}



// 邮箱改密码
- (void)youxianggaimian {
    
    
    
    if (!(self.oneTextField.text.length > 0)) {
            
           [MBProgressHUD showError:@"邮箱不能为空"];
            return;
        
      }else if (!(self.twoTextfeild.text.length > 0)) {
            
            
              [MBProgressHUD showError:@"验证码不能为空"];
              return;
            
            
      } else if (!(self.threderTextFeild.text > 0)){
          
           [MBProgressHUD showError:@"密码不能为空"];
                 return;
          
      }
        
        
        NSString *userID = [HWLocallyStoredData getLoginUserID];
        NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
        NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
        NSString *signMD5 = [WMUtil md5HexDigest:sign];
        NSDictionary *parametes = @{
                                     @"ct" :  @"app",
                                     @"ac" : @"re_passwd",
                                     @"ios" : @"1",
                                     @"email": self.oneTextField.text,
                                     @"code" : self.twoTextfeild.text,
                                     @"sessionid" : self.dataStr,
                                     @"passwd": self.threderTextFeild.text
                                     };
        
        
        
         
         [MBProgressHUD show:@"正在修改密码"];
         
         [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
             
           
             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
          //   [MBProgressHUD hideHUD];
             
             
             if ([codeStr intValue] == 1) {
               
           
                [MBProgressHUD  showSuccess:response[@"message"]];
              
                 
                  NSArray *Controller =   self.navigationController.childViewControllers;
                [self.navigationController popToViewController:Controller[Controller.count-3] animated:YES];
                 
                 
                 

             } else {
              
                  [MBProgressHUD  showSuccess:response[@"message"]];
                 
             }
             NSLog(@"%@",response);
             
             
         } failure:^(NSString *error) {

             [MBProgressHUD  showError:@"修改成功"];

             
         }];
         
        
    
    
    
    
}














- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
    
    static NSString *cellID = @"cellID";
    
    HWGuanlianPhoneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[HWGuanlianPhoneTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
    }
    
    if (indexPath.row == 0) {
        
        if ([self.panduanStr isEqualToString:@"1"]) {
            cell.namelabel.text = @"手机号码";
            cell.namelabel.text = [HWjianzhiduiObject keylocalization:@"手机号码"];
            
        } else {
            cell.namelabel.text = @"邮箱号";
               //   cell.codeBut.hidden = YES;
            cell.namelabel.text = [HWjianzhiduiObject keylocalization:@"邮箱号"];
        }
        
       // cell.namelabel.text = @"邮箱号码";
        cell.codeBut.hidden = YES;
        NSMutableAttributedString *attrString = nil;
        
         if ([self.panduanStr isEqualToString:@"1"]) {
             
             NSString  *shuru =  [HWjianzhiduiObject keylocalization:@"请输入手机号码"];
             
             
//             attrString = [[NSMutableAttributedString alloc] initWithString:@"请输入手机号码" attributes:
//                            @{NSForegroundColorAttributeName:RGB(153, 153, 153),
//                              NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}
//                            ];
             attrString = [[NSMutableAttributedString alloc] initWithString:shuru attributes:
                            @{NSForegroundColorAttributeName:RGB(153, 153, 153),
                              NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}
                            ];
             
             
             
             
         } else {
             
             
             
             NSString *youxiang = [HWjianzhiduiObject keylocalization:@"请输入邮箱"];
             
//              attrString = [[NSMutableAttributedString alloc] initWithString:@"请输入邮箱" attributes:
//                               @{NSForegroundColorAttributeName:RGB(153, 153, 153),
//                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}
//                               ];
             attrString = [[NSMutableAttributedString alloc] initWithString:youxiang attributes:
                               @{NSForegroundColorAttributeName:RGB(153, 153, 153),
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}
                               ];
             
         }
        
//           attrString = [[NSMutableAttributedString alloc] initWithString:@"请输入手机号码" attributes:
//                   @{NSForegroundColorAttributeName:RGB(153, 153, 153),
//                     NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}
//                   ];
//
        
        cell.titleTextField.attributedPlaceholder  = attrString;
        self.oneTextField = cell.titleTextField;
        
        
    } else if (indexPath.row == 1) {
        
         cell.namelabel.text = @"验证码";
         cell.namelabel.text = [HWjianzhiduiObject keylocalization: @"验证码"];
        
        
         cell.codeBut.hidden = NO;
//         NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"请输入验证码" attributes:
//                         @{NSForegroundColorAttributeName:RGB(153, 153, 153),
//                           NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}
//                         ];
//
        
        
        NSString *yanzhengma = [HWjianzhiduiObject keylocalization:@"请输入验证码"];
        
        NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yanzhengma attributes:
                        @{NSForegroundColorAttributeName:RGB(153, 153, 153),
                          NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}
                        ];
        
         cell.titleTextField.attributedPlaceholder  = attrString;
        
          self.twoTextfeild = cell.titleTextField;
        
        
        
        
    } else   {
        
        
        cell.namelabel.text = @"新密码";
        cell.namelabel.text = [HWjianzhiduiObject keylocalization:@"新密码"];
        
        
        cell.codeBut.hidden = YES;
//        NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"请输入新密码" attributes:
//                               @{NSForegroundColorAttributeName:RGB(153, 153, 153),
//                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}
//                               ];
        
         NSString *xinmima =   [HWjianzhiduiObject keylocalization:@"请输入新密码"];
        
        NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:xinmima  attributes:
                               @{NSForegroundColorAttributeName:RGB(153, 153, 153),
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}
                               ];
        
        
        
       cell.titleTextField.attributedPlaceholder  = attrString;
              
        self.threderTextFeild = cell.titleTextField;
        
        
    }
    
    
    
    [cell.codeBut addTarget:self action:@selector(xianshyanjTap:) forControlEvents:UIControlEventTouchUpInside];

    return  cell;
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return  44;
    
}





// 获取验证码
- (void)xianshyanjTap:(UIButton *)sende {
    
//    if (![WMUtil validateMobile:self.titleTextField.text]) {
//
//        [MBProgressHUD showError:@"请输入正确的手机号"];
//        return ;
//
//
//    }
//
    
    
    

    
    
    
    [Countdown  StartTimeCodeBtn:sende];
    if (!(self.oneTextField.text.length > 0)) {
        
        if ([self.panduanStr   isEqualToString:@"1"]) {
            
            [MBProgressHUD showError:@"手机号不能为空"];
            return;
            
        }
        [MBProgressHUD showError:@"邮箱不能为空"];
        return;
    }
   
    
    [self guanlianyouxiang];
    
    DLog(@"获取验证码");
    

    
}



@end
